namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProductManagePage : Page
    {
        protected Image iRegionManage;
        protected UserPrincipal Principal = null;
        protected DataList ProductDL;

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("ProductManage", str, language, path);
        }

        private void InitializeComponent()
        {
            this.ProductDL.ItemCommand += new DataListCommandEventHandler(this.ProductDL_ItemCommand);
            this.ProductDL.CancelCommand += new DataListCommandEventHandler(this.ProductDL_CancelCommand);
            this.ProductDL.EditCommand += new DataListCommandEventHandler(this.ProductDL_EditCommand);
            this.ProductDL.UpdateCommand += new DataListCommandEventHandler(this.ProductDL_UpdateCommand);
            this.ProductDL.DeleteCommand += new DataListCommandEventHandler(this.ProductDL_DeleteCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.SetFromImage();
                this.ProductDLDataBind();
            }
        }

        private void ProductDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.ProductDL.EditItemIndex = -1;
            this.ProductDLDataBind();
        }

        private void ProductDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            ManageProduct.RemoveProductViaId((Guid) this.ProductDL.DataKeys[e.Item.ItemIndex]);
            this.ProductDL.EditItemIndex = -1;
            this.ProductDLDataBind();
        }

        private void ProductDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.ProductDL.EditItemIndex = e.Item.ItemIndex;
            this.ProductDLDataBind();
        }

        private void ProductDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewName");
                TextBox SCNameTbx = (TextBox) e.Item.FindControl("txtNewSCName");
                if ((NameTbx != null) && (SCNameTbx != null))
                {
                    string ProductName = NameTbx.Text.Trim();
                    string ProductSCName = SCNameTbx.Text.Trim();
                    if ((ProductName != "") && (ProductSCName != ""))
                    {
                        if (ManageProduct.IsExistSameProductName(ProductName))
                        {
                            base.Response.Write("<script> alert('已存在该产品') </script>");
                            this.ProductDLDataBind();
                        }
                        else
                        {
                            ManageProduct.InsertNewProduct(ProductName, ProductSCName);
                            this.ProductDLDataBind();
                        }
                    }
                }
            }
        }

        private void ProductDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
            TextBox txtSCName = e.Item.FindControl("txtSCName") as TextBox;
            if ((txtName != null) && (txtSCName != null))
            {
                string ProductName = txtName.Text.Trim();
                string ProductSCName = txtSCName.Text.Trim();
                if (!(ProductName == "") && !(ProductSCName == ""))
                {
                    ManageProduct.UpdateProduct(ProductName, ProductSCName, (Guid) this.ProductDL.DataKeys[e.Item.ItemIndex]);
                    this.ProductDL.EditItemIndex = -1;
                    this.ProductDLDataBind();
                }
            }
        }

        private void ProductDLDataBind()
        {
            this.ProductDL.DataSource = ManageProduct.GetProduct();
            this.ProductDL.DataBind();
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iRegionManage.ImageUrl = "images/title_ProductManage.jpg";
                    break;

                case "en":
                    this.iRegionManage.ImageUrl = "images/title_ProductManageEn.jpg";
                    break;
            }
        }
    }
}

